package so.wy.servicecenter.businessbed.api.order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import so.wy.servicecenter.base.exception.WebException;
import so.wy.servicecenter.base.frame.api.AbstractTokenHttp;
import so.wy.servicecenter.base.frame.api.httpexception.HttpExceptionHandler;
import so.wy.servicecenter.businessbed.web.order.OrderOptWeb;

@RestController
@Scope("request")
@RequestMapping("/order/opt")
public class OrderOptHttp extends AbstractTokenHttp {
    @Autowired
    private OrderOptWeb orderOptWeb;

    @HttpExceptionHandler
    @PostMapping("/finish")
    public String finishOrder(String orderSerial) throws WebException {
        orderOptWeb.finishOrder(getUserId(), orderSerial);
        return toHttpJson();
    }

    @HttpExceptionHandler
    @PostMapping("/pre/pay/token")
    public String payPreOrder(String orderserial) throws WebException {
        return toHttpJson(orderOptWeb.payPreOrder(getUserId(), orderserial));
    }

    //人工免单
    @HttpExceptionHandler
    @PostMapping("/free")
    public String freeOrder(String orderSerial) throws WebException {
        orderOptWeb.freeOrder(getUserId(), orderSerial);
        return toHttpJson();
    }

    //租赁
    @HttpExceptionHandler
    @PostMapping("/rent/token")
    public String rentDevice(String deviceSerial) throws WebException {
        return toHttpJsonKV("orderSerial", orderOptWeb.rentDevice(getUserId(), deviceSerial));
    }

    //开锁失败
    @HttpExceptionHandler
    @PostMapping("/block/error/token")
    public String rentError(String orderSerial) throws WebException {
        orderOptWeb.rentError(getUserId(), orderSerial);
        return toHttpJson();
    }

    //归还
    @HttpExceptionHandler
    @PostMapping("/return/token")
    public String returnDevice(String deviceSerial) throws WebException {
        orderOptWeb.returnDevice(getUserId(), deviceSerial);
        return toHttpJson();
    }
}
